package streamingStudy.testStreaming

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

object WordCountWindows {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("WordCountWindows").setMaster("local[*]")
    //设置关闭
    conf.set("spark.streaming.stopGracefullyOnShutdown", "true")
    val ssc = new StreamingContext(conf, Seconds(3))
    ssc.checkpoint("./ck")

    val lines = ssc.socketTextStream("hadoop-01", 9999)
    val words = lines.flatMap(_.split(" "))
    val pairs = words.map((_, 1))
    val wordCounts = pairs.reduceByKeyAndWindow((a: Int, b: Int) => (a + b), Seconds(12), Seconds(6))
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }

}
